package com.bestcem.xm.member.mq.receiver;//package com.bestcem.xm.member.mq.receiver;
//
//import com.bestcem.xm.member.mq.message.SyncMemberStoreMessage;
//import com.bestcem.xm.member.service.MemberUserGroupService;
//import com.bestcem.xm.user.grpc.v1.services.BpUserServiceGrpc;
//import com.bestcem.xm.util.GrpcClientNames;
//import com.bestcem.xm.util.MsgPackFormatUtil;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import net.devh.boot.grpc.client.inject.GrpcClient;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.Base64;
//
///**
// * 监听用户层级变动队列
// * @author dc.wang <dc.wang@idiaoyan.com>
// * @version V1.0
// * @date 2022/2/11 16:24
// */
//@Slf4j
//@Component
//public class UserGroupChangedReceiver {
//    private static final Logger EMAIL_LOG = LoggerFactory.getLogger("EmailErrorLog");
//
//    @Resource
//    private MemberUserGroupService memberUserGroupService;
//
//    @GrpcClient(GrpcClientNames.XM)
//    private BpUserServiceGrpc.BpUserServiceBlockingStub bpUserServiceBlockingStub;
//
//    /**
//     * 监听用户层级变动队列
//     * @param message
//     * @param channel
//     * @throws IOException
//     */
//    @RabbitListener(queues = "#{groupChangedQueue.name}", ackMode = "MANUAL")
//    public void receive(Message message, Channel channel) throws IOException {
//        if (log.isInfoEnabled()) {
//            log.info("【联系人】开始同步用户层级数据");
//        }
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(message.getBody());
//            if (mqMessage == null) {
//                EMAIL_LOG.error("【联系人】用户层级变动消息解析失败: {}", Base64.getEncoder().encodeToString(message.getBody()));
//                channel.basicNack(deliveryTag, false, false);
//                return;
//            }
//
//            SyncMemberStoreMessage syncMemberStoreMessage =
//                    MsgPackFormatUtil.mapToObj(mqMessage.getData(), SyncMemberStoreMessage.class);
//            if (log.isInfoEnabled()) {
//                log.info("【联系人】同步消息:{}", syncMemberStoreMessage);
//            }
//
//            memberUserGroupService.flush(syncMemberStoreMessage.getOrgId());
//
//            channel.basicAck(deliveryTag, false);
//        } catch (Exception e) {
//            EMAIL_LOG.error("【联系人】处理倍市得app的mq消息异常:{}", Base64.getEncoder().encodeToString(message.getBody()), e);
//            try {
//                channel.basicNack(deliveryTag, false, false);
//            } catch (Exception e2) {
//                EMAIL_LOG.error("【联系人】处理倍市得app的mq消息, nack mq消息异常: {}",
//                        Base64.getEncoder().encodeToString(message.getBody()), e2);
//            }
//        }
//    }
//
//}
